<?php
App::uses('AppController', 'Controller');
/**
 * Questions Controller
 *
 * @property Question $Question
 */
class QuestionsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index($surveyId = null) {
		$this->Question->Survey->id = $surveyId;
		if (!$this->Question->Survey->exists()) {
			throw new NotFoundException("Invalid Survey ID");
		}

		$this->Question->Survey->recursive = -1;
		$this->Question->recursive = 1;
		$survey = $this->Question->Survey->findById($surveyId);

		$questions = $this->Question->find('all', array(
			'conditions' => array(
				'Question.survey_id' => $surveyId
			),
			'order' => array('Question.numeral ASC'),
		));

		$this->set(compact('survey', 'questions'));
	}

/**
 * add method
 *
 * @return void
 */
	public function add($surveyId) {
		$this->Question->Survey->id = $surveyId;
		if (!$this->Question->Survey->exists()) {
			throw new NotFoundException("Invalid Survey ID");
		}

		if ($this->request->is('post') || $this->request->is('put')) {
			$this->Question->create();
			$this->Question->set('survey_id', $surveyId);

			$concreteQuestionType = $this->request->data['Question']['type'];

			$dataSource = $this->Question->getDataSource();
			$dataSource->begin();

			if (!$this->Question->save($this->request->data)) {
				$dataSource->rollback();
				$this->Flash->error("Could not save Question!");
				return;
			}

			$concreteQuestion = $this->Question->$concreteQuestionType;
			$concreteQuestion->create();
			$concreteQuestion->set('question_id', $this->Question->id);

			if (!$concreteQuestion->save()) {
				$dataSource->rollback();
				$this->Flash->error("Could not save Question!");
				return;
			}

			$dataSource->commit();
			return $this->redirect(array('action' => 'edit', $this->Question->id));

		}
		$question = $this->Question->findById($this->Question->id);
		$questionTypes = $this->Question->questionTypes;
		$this->set(compact('questionTypes'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Question->id = $id;
		if (!$this->Question->exists()) {
			throw new NotFoundException('Invalid Question ID');
		}

		$question = $this->Question->findById($id);
		$cq = $question['ConcreteQuestion'];

		$this->set(compact('question'));

		if ($this->request->is('post') || $this->request->is('put')) {
			$dataSource = $this->Question->getDataSource();
			$dataSource->begin();

			//Update question
			if (!$this->Question->save($this->request->data['Question'])) {
				$dataSource->rollback();
				$this->Flash->error("There was an error saving this Question.");
				return;
			}

			//Update concrete question subclass
			if (!$cq->save($this->request->data[$cq->name])) {
				$dataSource->rollback();
				$this->Flash->error("There was an error saving this Question.");
				return;
			}

			$dataSource->commit();
			return $this->redirect(array('controller' => 'Questions', 'action' => 'index', $question['Question']['survey_id']));

		} else {
			$this->request->data = $question;
			$cqById = $cq->findById($cq->id);
			$this->request->data[$cq->name] = $cqById[$cq->name];
			unset($this->request->data['ConcreteQuestion']);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Question->id = $id;
		$surveyIdResult = $this->Question->read('survey_id');
		$surveyId = $surveyIdResult['Question']['survey_id'];

		if (!$this->Question->exists()) {
			throw new NotFoundException(__('Invalid question'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Question->delete()) {
			$this->Flash->success("Question deleted!");
			$this->redirect(array('action' => 'index', $surveyId));
		}
		$this->Flash->error("The Question could not be deleted.");
		$this->redirect(array('action' => 'index', $surveyId));
	}

	public function changeOrder($surveyId = null) {
		$this->Question->Survey->id = $surveyId;
		if (!$this->Question->Survey->exists()) {
			throw new NotFoundException("Invalid Survey ID");
		}

		$this->Question->Survey->recursive = -1;
		$this->Question->recursive = 1;

		$survey = $this->Question->Survey->findById($surveyId);
		$questions = $this->Question->find('all', array(
			'conditions' => array(
				'Question.survey_id' => $surveyId
			),
			'order' => array('Question.numeral ASC'),
		));
		$this->set(compact('survey', 'questions'));

		if ($this->request->is('post') || $this->request->is('put')) {
			//Save post data, redirect to index
			$numeral = 1;
			$dataSource = $this->Question->getDataSource();
			$dataSource->begin();

			foreach ($this->request->data['questions'] as $questionId) {
				$this->Question->id = $questionId;
				if (!$this->Question->exists()) {
					throw new NotFoundException("Invalid Question ID");
				}

				$this->Question->set('numeral', $numeral);
				$numeral++;
				if (!$this->Question->save()) {
					$this->Flash->error("There was an error saving the new Question order.");
					return $this->redirect(array('action' => 'index', $surveyId));
				}
			}
			
			//All OK
			$dataSource->commit();
			return $this->redirect(array('action' => 'index', $surveyId));
		}
		
	}
}
